package com.qdxwx.web.controller.user;

import com.qdxwx.common.config.UploadConfig;
import com.qdxwx.common.upload.UploadState;
import com.qdxwx.common.upload.UploadUtils;
import com.qdxwx.web.controller.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ZhiYong
 * @createTime 2021/2/20
 * @description
 */
@RestController
@RequestMapping("/user/upload")
public class UserUploadController extends BaseController {

    private static Logger logger = LoggerFactory.getLogger(UserUploadController.class);

    /**
     * 编辑器图片上传，原图保存
     *
     * @param request request
     * @return String
     */
    @RequestMapping(value = "/images.json", method = {RequestMethod.POST})
    public Map<String, Object> imageUpload(HttpServletRequest request) {

        UploadState uploadState = UploadUtils.saveImageFile(request);

        return uploadState.getResult();

    }

    @RequestMapping(value = "/cover.json", method = {RequestMethod.POST})
    public Map<String, Object> coverUpload(HttpServletRequest request) {
        UploadState uploadState = UploadUtils.saveCoverFile(request);
        return uploadState.getResult();
    }

    @RequestMapping(value = "/avatar.json", method = {RequestMethod.POST})
    public Map<String, Object> avatarUpload(HttpServletRequest request) {
        UploadState uploadState = UploadUtils.saveAvatarFile(request);
        return uploadState.getResult();
    }

    /**
     * 上传文件
     *
     * @param request request
     * @return String
     */
    @RequestMapping(value = "/file.json")
    public Map<String, Object> uploadFile(HttpServletRequest request) {
        UploadState uploadState = UploadUtils.saveFile(request);
        return uploadState.getResult();
    }

    /**
     * 上传文件
     *
     * @param request request
     * @return String
     */
    @RequestMapping(value = "/temp/file.json")
    public Map<String, Object> uploadFileTemp(HttpServletRequest request) {
        UploadState uploadState = UploadUtils.saveFile(request, UploadConfig.uploadTempFolder);
        return uploadState.getResult();
    }

    /**
     * 上传文件
     *
     * @param multipartFile multipartFile
     * @return String
     */
    @RequestMapping(value = "/temp/image.json")
    public Map<String, Object> uploadImageTemp(MultipartFile multipartFile) {
        UploadState uploadState = UploadUtils.saveImageFile(multipartFile, UploadConfig.uploadTempFolder);
        return uploadState.getResult();
    }
}
